import pyWinhook as pyHook
import pythoncom

# 记录按键按下、释放
class KeyBoardManager():
    keyIsPressed = False
    def onKeyDown(self,event):
        if self.keyIsPressed:
            return True
        print(str(event.Key) + ' is pressed')
        self.keyIsPressed = True
        return True

    def onKeyUp(self,event):
        self.keyIsPressed = False
        print(str(event.Key) + ' is released')
        return True

if __name__ == '__main__':
    mykbmanager = KeyBoardManager()
    hookmanager = pyHook.HookManager()
    hookmanager.KeyDown = mykbmanager.onKeyDown
    hookmanager.KeyUp = mykbmanager.onKeyUp
    hookmanager.HookKeyboard()
    pythoncom.PumpMessages()